package com.kensure.shike.zhang.service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.exception.ParamUtils;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.ArithmeticUtils;
import co.kensure.mem.MapUtils;

import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.service.SKUserService;
import com.kensure.shike.zhang.dao.SKUserYueDao;
import com.kensure.shike.zhang.model.SKUserYue;
import com.kensure.shike.zhang.model.SKUserZhang;

@Service
public class SKUserYueService extends JSBaseService {

	@Resource
	private SKUserYueDao dao;

	@Resource
	private SKUserService sKUserService;
	@Resource
	private SKUserZhangService sKUserZhangService;

	public SKUserYue selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<SKUserYue> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<SKUserYue> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKUserYue obj) {
		super.beforeInsert(obj);
		if (obj.getJinbi() == null) {
			obj.setJinbi(0D);
		}
		if (obj.getYue() == null) {
			obj.setYue(0D);
		}
		if (obj.getDbyue() == null) {
			obj.setDbyue(0D);
		}
		return dao.insert(obj);
	}

	private boolean updateByMap(Map<String, Object> params) {
		params.put("updatedTime", new Date());
		return dao.updateByMap(params);
	}

	/**
	 * 查询用户余额
	 * 
	 * @return
	 */
	public SKUserYue selectByUser() {
		SKUser user = sKUserService.getUser();
		SKUserService.checkUser(user);
		SKUserYue s = selectOne(user.getId());
		return s;
	}

	/**
	 * 通过账单修改余额，顺便会修改账单状态
	 * 
	 * @param obj
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public synchronized void updateYue(Long zhangid) {
		SKUserZhang zhang = sKUserZhangService.selectOne(zhangid);
		ParamUtils.isBlankThrewException(zhang.getUserid(), "用户为空");
		SKUserYue sksy = selectOne(zhang.getUserid());
		if (sksy == null) {
			sksy = new SKUserYue();
			sksy.setDbyue(0D);
			sksy.setJinbi(0D);
			sksy.setYue(0D);
			sksy.setId(zhang.getUserid());
			insert(sksy);
		}
		if (zhang.getStatus() == 0) {
			double yue = zhang.getYue() * zhang.getInorout();
			double jinbi = zhang.getJinbi() * zhang.getInorout();
			double x = ArithmeticUtils.add(yue, sksy.getYue());
			double z = ArithmeticUtils.add(jinbi, sksy.getJinbi());
			if (x < 0) {
				BusinessExceptionUtil.threwException("用户余额不足");
			}
			if (z < 0) {
				BusinessExceptionUtil.threwException("用户金币不足");
			}
			Map<String, Object> params = MapUtils.genMap("id", zhang.getUserid(), "yueAdd", yue, "jinbiAdd", jinbi);
			updateByMap(params);
			zhang.setStatus(1);
			sKUserZhangService.update(zhang);
		}

	}

}
